package com.alvin.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alvin.module.vo.WechatVo;
import com.alvin.springboot.dao.UserDAOImpl;
import com.alvin.springboot.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @Author: Alvin
 * @Project_name: SpringbootTest
 * @Package_name: com.alvin.springboot.controller
 * @Type_name: IndexController
 * @DATE: 2017/2/10
 */
@Controller
@RequestMapping(value = "/")
public class IndexController {
    @Autowired
    UserDAOImpl userDAOImpl;

    private final Logger log = LoggerFactory.getLogger(this.getClass());


    @RequestMapping(value = "/")
    public String i(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", "hi,hello world");
        return "index";
    }

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }
    @RequestMapping(value = "/login")
    public String login() {
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/loginReq")
    public void loginReq(HttpServletRequest request,HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {   //使用ajax调用的返回
            User user = userDAOImpl.findById(username);
            if(user == null){
                System.out.println("用户不存在!");
                response.getWriter().print("用户不存在!");
                return;
            }else if(!password.equals(user.getPassword())){
                System.out.println("密码错误!");
                response.getWriter().print("密码错误!");
                return;
            }
            HttpSession session = request.getSession();
            session.setAttribute("usersession",user);
            response.getWriter().print("success");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value="/requestMQ")
    public void requestQueue(HttpServletRequest request,HttpServletResponse response){
    }

    @ResponseBody
    @RequestMapping(value="/getWechat", method = RequestMethod.GET)
    public String validateWechat(WechatVo vo){
        String str = "alvinlovelife" + vo.getTimestamp() + vo.getNonce();
        log.info("【wechat】:{}", JSON.toJSONString(vo));
        System.out.println("1234");
        return vo.getEchostr();
    }
}